package com.financing.wap.web.controller.adminapi;

import com.financing.wap.common.Pager;
import com.financing.wap.common.ResultMap;
import com.financing.wap.service.ICommonService;
import com.financing.wap.util.DateUtil;
import com.financing.wap.vo.admin.AdminArticleInfoVO;
import com.financing.wap.web.controller.BaseController;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller("adminStatisticsController")
@RequestMapping("/adminapi/statistics")
public class StatisticsController extends BaseController  {

	private Logger logger = Logger.getLogger(getClass());
    
    @Autowired
    private ICommonService commonService;

	
	@RequestMapping("/operationStatistics.shtml")
	@ResponseBody
	public ResultMap operationStatistics(HttpServletRequest request, AdminArticleInfoVO dstArticle, Pager pager){
		ResultMap result = new ResultMap();
		
		try{

			logger.info("query operationStatistics list begin .......");

            Map<String,String> papMap = new HashMap<String, String>();
			String dateStart = request.getParameter("dateStart");
			String dateEnd = request.getParameter("dateEnd");
			String type = request.getParameter("type");
			type = StringUtils.isBlank(type)?"7":type;

			if(StringUtils.isBlank(dateStart) ||  StringUtils.isBlank(dateEnd)) {
				if (type.equals("7")) {
					dateEnd = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
					dateStart = DateUtil.formatDate(DateUtil.addDay(new Date(), -7), "yyyy-MM-dd");
				} else {
					dateEnd = DateUtil.formatDate(new Date(), "yyyy-MM-dd");
					dateStart = DateUtil.formatDate(DateUtil.addDay(new Date(), -30), "yyyy-MM-dd");
				}
			}

			papMap.put("createDateStart",dateStart);
			papMap.put("createDateEnd",dateEnd);

            Map<String,String>  allList = commonService.queryOperationStatisticsForAll(papMap);
            List< Map<String,String> > dailyDischargeList = commonService.queryOperationStatisticsForDailyDischarge(papMap);
            List< Map<String,String> > articleTopList = commonService.queryOperationStatisticsForDailyDischargeUser(papMap);
            List< Map<String,String> > articleTrendList = commonService.queryOperationStatisticsForArticleTrend(papMap);
			List< Map<String,String> > articleTypeList = commonService.queryOperationStatisticsForArticleType(papMap);

			result.setAttribute("allList",allList);
            result.setAttribute("dailyDischargeList",dailyDischargeList);
            result.setAttribute("articleTopList",articleTopList);
            result.setAttribute("articleTrendList",articleTrendList);
			result.setAttribute("articleTypeList",articleTypeList);

			result.setAttribute("pager", pager);
			result.setStatusCode(ResultMap.STATUS_CODE_SUCCESS);
			result.setStatus(ResultMap.STATUS.SUCCESS);
			
			logger.info("query operationStatistics list end .......");
		}catch(Exception e){
			log.error("query operationStatistics error!",e);
			result.setStatus(ResultMap.STATUS.ERROR);
			result.setStatusCode(ResultMap.STATUS_CODE_SYS_EXCEPTION);
		}
		
		return result;
	}

}
